﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGrease.Css.Extensions;
using YunSpace.Application.SiteLibraryManage;
using YunSpace.Application.SiteMgr;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.ViewModel.Sale;
using YunSpace.Domain.ViewModel.SiteMgr.SalesTarget;

namespace YunSpace.Web.Areas.ClientManager.Controllers
{
    /// <summary>
    /// 销售指标
    /// </summary>
    public class SaleTargetController : ControllerBase
    {
        private readonly UserApp _userApp = new UserApp();
        private readonly OrganizeApp _organizeApp = new OrganizeApp();
        private readonly SiteSalesApp _siteSalesApp = new SiteSalesApp();
        private readonly ActivitiesCaseApp _activitiesCaseApp = new ActivitiesCaseApp();

        #region 月指标设置

        /// <summary>
        /// 获取月指标列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetIndexGridJson(Pagination pagination, string queryJson)
        {
            var request = queryJson.ToObject<GetMonthTargetGridRequest>();
            return Content(_siteSalesApp.GetMonthTargetGridDtos(pagination, request).ToGridJson(pagination));
        }

        /// <summary>
        /// 编辑月指标
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditMonthTarget()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetDepartmentsJson()
        {
            return Success(_organizeApp.GetList().Where(x => x.F_CategoryId == "Department"));
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSalersJson(string departmentId)
        {
            return Success(_userApp.GetUsersBy(departmentId));
        }

        [HttpPost]
        public ActionResult SaveMonthTarget(SiteSalesTargetSettingEntity entity, string keyValue)
        {
            var result = _siteSalesApp.SaveSiteSalesTargetSetting(entity, keyValue);
            return result ? Success("操作成功。") : Error("已经存在相同的数据，【销售人员】、【部门】、【年】和【月】的组合必须是唯一的");
        }

        [HttpPost]
        public ActionResult UpThreshold(string keyValue)
        {
            return Success(keyValue);
        }

        [HttpPost]
        public ActionResult UpCellValue(string Uid,string Month,string Year,decimal CellValue)
        {
            var result = _siteSalesApp.EditSiteSalesTargetSetting(Uid,Year,Month,CellValue);
            return result ? Success("") : Error("还未设置月指标");
        }

        [HttpPost]
        public ActionResult DelThreshold(string Uid,string Year)
        {
            var result = _siteSalesApp.DelSiteSaleTargetSetting(Uid, Year);
            return result ? Success("") : Error("操作失败");
        }

        /// <summary>
        /// 获取目标值
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetThresholdJson(string queryJson)
        {
            var request = queryJson.ToObject<GetMonthTargetGridRequest>();
            return Success(_siteSalesApp.GetThresholdsBy(request));
        }

        #endregion 月指标设置

        #region 目标查看

        /// <summary>
        /// 目标查看
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LookTarget()
        {
            return View();
        }

        /// <summary>
        /// 获取目标查看列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetLookTargetGridJson(Pagination pagination, string queryJson)
        {
            var request = queryJson.ToObject<GetLookTargetGridJsonRequest>();
            return Content(_siteSalesApp.GetLookTargetViewModels(pagination, request).ToGridJson(pagination));
        }

        #endregion 目标查看

        #region 活动台账

        /// <summary>
        /// 活动台账
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ActivitiesCaseBill()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DrawActivitiesCaseBill()
        {
            return View();
        }

        /// <summary>
        /// 获取活动台账列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetActivitiesCaseBillGridJson(Pagination pagination, string queryJson)
        {
            var request = queryJson.ToObject<GetActivitiesCaseBillGridJsonRequest>();
            return Content(_activitiesCaseApp.GetActivitiesCaseBillDtos(pagination, request).ToGridJson(pagination));
        }

        #endregion 活动台账
    }
}